﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using YwangHRMSys.DAL;
using YwangHRMSys.DBUtility;
using YwangHRMSys.Model;


namespace YwangHRMSys.UI.LoginMgr
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();
        }

      
       
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            return;
        }

        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            string userName = UserNameTxt.Text;
            string userPassword = PasswordTxt.Password;

     
            OperatorInfoModel operatorInfo = new OperatorInfoDAL().GetByUserName(userName);
         
            if (operatorInfo == null)
                {
                    MessageBox.Show("用户名不存在！");
                }
            else
                {
                    string dbMd5 = operatorInfo.PassWord;//数据库中存储的密码值
                    string myMd5 = CommonHelper.GetMD5(userPassword+CommonHelper.GetPasswordSalt());
                  //登录成功
                if (dbMd5==myMd5)
                        {
                           new OperatorInfoDAL().UpdateUnLocked(operatorInfo.Id);

                            Application.Current.Properties["将登录用户信息存储到内存中"] = operatorInfo.Id;
                            DialogResult = true;
                        }
                    else
                        {
                        //如果密码错误则给LOCKED+1
                         new   OperatorInfoDAL().UpdateLocked(operatorInfo.Id);
                            MessageBox.Show("密码错误!\n现在已经错误"+operatorInfo.IsLocked+"次，错误5次账户将锁定。");
                        }


                }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            OperatorInfoModel[] admin = new OperatorInfoDAL().ListAll();
          int i=  admin.Count();
           
            if (i == 0)
            {
                OperatorInfoModel operatorInfoModel = new OperatorInfoModel();
                operatorInfoModel.Id = Guid.NewGuid();
                operatorInfoModel.UserName = "admin";

                operatorInfoModel.IsDeleted = false;
                operatorInfoModel.RealName = "ADMIN";
                string passwrod = "admin";
                operatorInfoModel.PassWord = CommonHelper.GetMD5(passwrod + CommonHelper.GetPasswordSalt());
                new OperatorInfoDAL().InsertOperatorInfo(operatorInfoModel);
                var adminModel = new AdminPermissionModel();
                adminModel.Id = operatorInfoModel.Id;
                adminModel.UserName = operatorInfoModel.UserName;
                adminModel.Permission = 0;
                new AdminPermissionDal().InsertAdminPermission(adminModel);
                //eab17cd8-dfd2-4f61-a2d1-024959ffebb7	admin	7e206e53b7f8a3638e40670135013054	False	梁桐铭	0	2014-07-14 19:00:50.000
                MessageBox.Show("亲，您好，这 是第一次运行。\n默认账号： admin\n默认密码为：admin\n祝您使用开心。\n开发者：梁桐铭 \nQQ:623037939");

            }
            
        }

        private void BtnFirst_Click(object sender, RoutedEventArgs e)
        {
       
        }
    }
}
